package com.example.armmvvm.ui.scrollview

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.core.widget.NestedScrollView

/**
 * 见[NestedScrollBehaviorHelper]
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.common.scrollview
 * @createTime 创建时间 ：2021/1/19
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class BehaviorNestedScrollView : NestedScrollView {

    val behaviorHelper = NestedScrollBehaviorHelper(this)

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray) {
        val interceptState = behaviorHelper.interceptNestedPreScroll(target, dx, dy, consumed)
        if (interceptState != 0) {
            if (interceptState == 2) {
                super.onNestedPreScroll(target, dx, dy, consumed)
            }
            behaviorHelper.handleNestedPreScroll(target, dx, dy, consumed)
        }
    }

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
        val interceptState = behaviorHelper.interceptNestedPreScroll(target, dx, dy, consumed)
        if (interceptState != 0) {
            if (interceptState == 2) {
                super.onNestedPreScroll(target, dx, dy, consumed, type)
            }
            behaviorHelper.handleNestedPreScroll(target, dx, dy, consumed)
        }
    }

    /**
     * 子视图的Fling
     */
    override fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean {
        if (!behaviorHelper.interceptNestedPreFling(target, velocityX, velocityY)) {
            val consumeFling = super.onNestedPreFling(target, velocityX, velocityY)
            return behaviorHelper.handleNestedPreFling(target, velocityX, velocityY, consumeFling)
        }
        return false
    }

    override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
        if (!behaviorHelper.interceptNestedScroll(target)) {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
        }
    }

    override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {
        if (!behaviorHelper.interceptNestedScroll(target)) {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type)
        }
    }

    override fun onNestedScroll(
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int,
        type: Int,
        consumed: IntArray
    ) {
        if (!behaviorHelper.interceptNestedScroll(target)) {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
        }
    }

    override fun onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean): Boolean {
        return if (!behaviorHelper.interceptNestedFling(target)) {
            super.onNestedFling(target, velocityX, velocityY, consumed)
        } else false
    }
}